package com.javasm.controller.B2CController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.http.AxiosResult;
import com.javasm.common.page.PageResult;
import com.javasm.controller.base.BaseController;
import com.javasm.domin.criteria.orderCriteria.OrderGoodsCriteria;
import com.javasm.domin.entity.goodsEntity.GdGoods;
import com.javasm.domin.entity.orderEntity.B2cOrderGoods;
import com.javasm.domin.entity.orderEntity.B2cShoppingcart;
import com.javasm.domin.vo.goodsVo.GoodsVo;
import com.javasm.domin.vo.orderVo.OrderGoodVo;
import com.javasm.domin.vo.orderVo.OrderShoppingVo;
import com.javasm.mapper.B2cOrderMapper.B2cShoppingcartMapper;
import com.javasm.mapper.goodsMapper.GdGoodsMapper;
import com.javasm.service.B2cOrderService.B2cShoppingcartService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.io.Serializable;
import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/11/25-20:40
 * @Since:jdk1.8
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/shopping")
public class OrderShoppingController extends BaseController {

    private final B2cShoppingcartMapper b2cShoppingcartMapper;
    private final B2cShoppingcartService b2cShoppingcartService;
    private final GdGoodsMapper gdGoodsMapper;

    @RequestMapping("/show")
    public AxiosResult<List<B2cShoppingcart>> find() {
        QueryWrapper<B2cShoppingcart> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("id");
        List<B2cShoppingcart> b2cShoppingcarts = b2cShoppingcartMapper.selectList(queryWrapper);
        return AxiosResult.success(b2cShoppingcarts);
    }

    /**
     * 添加购物车
     *
     * @param b2cShoppingcart
     * @return
     */
    @PostMapping("/add")
    public AxiosResult<Integer> add(@RequestBody B2cShoppingcart b2cShoppingcart) {
        int insert = b2cShoppingcartMapper.insert(b2cShoppingcart);
        System.out.println(AxiosResult.success(insert));
        return AxiosResult.success(insert);
    }

    /**
     * 批量添加
     *
     * @param goodArray
     * @return
     */
    @PostMapping("/addArray")
    public AxiosResult<Integer> addList(@RequestBody List<GoodsVo> goodArray) {
        B2cShoppingcart b2cShoppingcart = new B2cShoppingcart();
        int insert = 0;

        for (int i = 0; i < goodArray.size(); i++) {
            GoodsVo goodsVo = goodArray.get(i);
            b2cShoppingcart.setColourId(goodsVo.getColourId());
            b2cShoppingcart.setBrandId(goodsVo.getBrandId());
            b2cShoppingcart.setGoodsName(goodsVo.getGoodsName());
            b2cShoppingcart.setModelId(goodsVo.getModelId());
            b2cShoppingcart.setId(goodsVo.getId());
            b2cShoppingcart.setTypeId(goodsVo.getTypeId());
            b2cShoppingcart.setGoodPrice(goodsVo.getMallPrice());
            insert = b2cShoppingcartMapper.insert(b2cShoppingcart);
        }
        return AxiosResult.success(insert);
    }


    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    @DeleteMapping("delete/{idList}")
    public AxiosResult<Integer> delete(@PathVariable List<Serializable> idList) {
        int i = b2cShoppingcartMapper.deleteBatchIds(idList);
        return AxiosResult.success(i);
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public AxiosResult<Integer> del(@PathVariable Serializable id) {
        int i = b2cShoppingcartMapper.deleteById(id);
        return AxiosResult.success(i);
    }

    @RequestMapping("/showAll")
    public AxiosResult<List<OrderShoppingVo>> show() {
        List<OrderShoppingVo> shoppingList = b2cShoppingcartService.findShoppingList();
        return AxiosResult.success(shoppingList);
    }
}
